from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.core.config import settings

engine = create_engine(
    settings.DATABASE_URL,
    pool_pre_ping=True,  # 自动处理断开的连接
    pool_recycle=3600,   # 一小时后回收连接
    pool_size=5,         # 连接池大小
    max_overflow=10      # 最大溢出连接数
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 依赖项
def get_db():
    db = SessionLocal()
    try:
        print(settings.DATABASE_URL)
        yield db
    finally:
        db.close() 